package com.rainwood.water_authority.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.water_authority.R;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.helper.InputTextHelper;
import com.rainwood.water_authority.http.request.ShouLiaoDetilsApi;
import com.rainwood.water_authority.http.request.ShouLiaoDetilsUpdateApi;
import com.rainwood.water_authority.http.response.PanDianHistoryBean;
import com.rainwood.water_authority.http.response.SimpleResultBean;
import com.rainwood.water_authority.ui.adapter.HistoryPanDianAdapter;
import com.rainwood.water_authority.ui.adapter.InventoryIssueBaseAdapter;
import com.rainwood.water_authority.ui.dialog.MessageDialog;
import com.rainwood.water_authority.util.SoftHideKeyBoardUtils;
import com.sxs.easyhttp.EasyHttp;
import com.sxs.easyhttp.listener.HttpCallback;
import com.sxs.tools.titlebar.TitleBar;

import java.util.HashMap;

/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 收料物资数量
 */
public final class FaLiaoDetilsAct extends MyBaseActivity {
    public static void start(Context context, String id, String direction, String from) {
        Intent intent = new Intent(context, FaLiaoDetilsAct.class);
        intent.putExtra("id", id);
        intent.putExtra("direction", direction);
        intent.putExtra("from", from);
        context.startActivity(intent);
    }

    private RecyclerView mOutboundListView;
    private EditText mOutboundNumView;
    private TextView mIssueNameView;
    private TextView tv_planNum;
    private TextView tv_material_code;
    private TextView mModelView;
    private TextView mUnitView, tv_num, btn_outbound;
    private InventoryIssueBaseAdapter mInventoryIssueAdapter;
    private HistoryPanDianAdapter historyPanDianAdapter;
    private LinearLayout btn_scan_outbound;
    private PanDianHistoryBean panDianHistoryBean;
  private CheckBox checkBox;
  private TitleBar title;
  private TextView tv_num_title,tv_submit,tv_ready_type;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_shouliaodetils;
    }

    @Override
    protected void initView() {
        title=findViewById(R.id.title);
        title.setLeftTitle("填写发料数量");
        tv_num_title=findViewById(R.id.tv_num_title);
        tv_num_title.setText("发料数量");
        tv_submit=findViewById(R.id.tv_submit);
        tv_submit.setText("确认发料");
        tv_ready_type=findViewById(R.id.tv_ready_type);
        tv_ready_type.setText("已发料");
        mIssueNameView = (TextView) findViewById(R.id.tv_issue_name);
        tv_planNum = (TextView) findViewById(R.id.tv_planNum);
        tv_material_code = (TextView) findViewById(R.id.tv_material_code);
        mModelView = (TextView) findViewById(R.id.tv_model);
        mUnitView = (TextView) findViewById(R.id.tv_unit);
        tv_num = (TextView) findViewById(R.id.tv_num);
        btn_scan_outbound = findViewById(R.id.btn_scan_outbound);
        btn_outbound = findViewById(R.id.btn_outbound);
        btn_outbound.setVisibility(View.GONE);
        checkBox=findViewById(R.id.cb);


        mOutboundListView = findViewById(R.id.rv_outbound_list);
        mOutboundNumView = findViewById(R.id.et_outbound_num);
        mOutboundNumView.setHint("请填写发料数量");
        btn_scan_outbound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updata();
            }
        });

        mInventoryIssueAdapter = new InventoryIssueBaseAdapter(this);
        historyPanDianAdapter = new HistoryPanDianAdapter(this);
        mOutboundListView.setAdapter(historyPanDianAdapter);
        InputTextHelper.with(this)
                .addView(mOutboundNumView)
                .setMain(btn_scan_outbound)
                .build();
        SoftHideKeyBoardUtils.assistActivity(this);
    }

    private void updata() {

        String numStr = mOutboundNumView.getText().toString().trim();
        int num = Integer.parseInt(numStr);
        if (mInventoryIssueAdapter.getData() != null && Integer.parseInt(panDianHistoryBean.getData().getInfo().getStockNum()) < num) {
            new MessageDialog.Builder(this)
                    .setMessage("当前库存不足")
                    .setCancel(null)
                    .setListener(dialog -> {
                    }).show();
            return;
        }
        if (panDianHistoryBean!=null&&panDianHistoryBean.getData()!=null
                &&panDianHistoryBean.getData().getInfo()!=null
                && panDianHistoryBean.getData().getInfo().getMaterial() != null){
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("orderId",panDianHistoryBean.getData().getInfo().getOrderId());
            hashMap.put("code",panDianHistoryBean.getData().getInfo().getMaterial().getCode());
            hashMap.put("from",panDianHistoryBean.getData().getInfo().getSource());
            hashMap.put("direction", panDianHistoryBean.getData().getDirection());
                if (checkBox.isChecked()){
                    hashMap.put("num","-"+num);
                }else {
                    hashMap.put("num",num+"");
                }
            EasyHttp.post(this)
                    .api(new ShouLiaoDetilsUpdateApi()
                            .setId(hashMap))
                    .request(new HttpCallback<SimpleResultBean>(this) {
                        @Override
                        public void onSucceed(SimpleResultBean result) {
                            toast(result.getWarn());
                            if (result.getCode() == 1) {
                                postDelayed(() -> finish(), 1000);
                            }
                        }
                    });
        }

    }

    @Override
    protected void initData() {
        if (getString("id") != null && !getString("id").equals("") && getString("direction") != null &&
                getString("from") != null) {
            HashMap hashMap = new HashMap();
            hashMap.put("id", getString("id"));
            hashMap.put("direction", getString("direction"));
            hashMap.put("from", getString("from"));
            EasyHttp.post(this)
                    .api(new ShouLiaoDetilsApi().setId(hashMap))
                    .request(new HttpCallback<PanDianHistoryBean>(this) {
                        @Override
                        public void onSucceed(PanDianHistoryBean result) {
                            panDianHistoryBean = result;
                            if (result.getData().getInfo() != null) {
                                mIssueNameView.setText(result.getData().getInfo().getMaterial().getName());
                                tv_planNum.setText(result.getData().getInfo().getPlanNum());
                                tv_num.setText(result.getData().getInfo().getNum());
                                tv_material_code.setText(result.getData().getInfo().getMaterial().getCode());
                                mModelView.setText(result.getData().getInfo().getMaterial().getModel());
                                mUnitView.setText(result.getData().getInfo().getMaterial().getUnit());
                            }
                            if (result.getData() != null && result.getData().getRecord() != null) {
                                historyPanDianAdapter.clearData();
                                historyPanDianAdapter.setData(result.getData().getRecord());
                            }
                        }
                    });
        }


//        EasyHttp.post(this)
//                .api(("scan".equals(getString(IntentKey.TYPE))
//                        ? new InventoryScanApi().setCsId(getString(IntentKey.ID))
//                        : new InventoryIssueApi().setGoodsId(getString(IntentKey.ID))))
//                .request(new HttpCallback<InventoryIssueBean>(this) {
//                    @Override
//                    public void onSucceed(InventoryIssueBean result) {
//                        if (result.getData() != null) {
//                            if (result.getCode() == 0) {
//                                toast(result.getWarn());
//                                ErrorPageAty.start(getContext(), "盘点扫码失败");
//                                finish();
//                                return;
//                            }
//                           /* if (!"inventory".equals(result.getData().getCType())){
//                                toast("非盘点物资");
//                                postDelayed(() -> finish(), 1000);
//                                return;
//                            }*/
//                            InventoryIssueBean.DataDTO resultData = result.getData();
//                            List<InventoryDetailBean.DataDTO.GoodsDTO> goodsDTOList = new ArrayList<>();
//                            InventoryDetailBean.DataDTO.GoodsDTO goodsDTO = new InventoryDetailBean.DataDTO.GoodsDTO();
//                            goodsDTO.setGoodsId(resultData.getGoodsId());
//                            goodsDTO.setName(resultData.getName());
//                            goodsDTO.setNowKuNum(resultData.getNowKuNum());
//                            goodsDTO.setNumber(resultData.getNumber());
//                            goodsDTO.setSpeciModel(resultData.getSpeciModel());
//                            goodsDTO.setUnit(resultData.getUnit());
//                            goodsDTOList.add(goodsDTO);
//                            mInventoryIssueAdapter.clearData();
//                            mInventoryIssueAdapter.setData(goodsDTOList);
//                        } else {
//                            if (result.getCode() == 0) {
//                                toast(result.getWarn());
//                                ErrorPageAty.start(getContext(), "盘点扫码失败");
//                                finish();
//                            }
//                        }
//                    }
//                });
    }


}
